package com.canaan.business.controller.pc.quality;

import java.util.List;

import com.canaan.business.common.enums.quality.QualityValueTypeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.canaan.common.annotation.Log;
import com.canaan.common.core.controller.BaseController;
import com.canaan.common.core.domain.AjaxResult;
import com.canaan.common.enums.BusinessType;
import javax.annotation.Resource;
import com.canaan.business.domain.quality.QualityItem;
import com.canaan.business.service.quality.IQualityItemService;
import com.canaan.common.utils.poi.ExcelUtil;
import com.canaan.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 质检项Controller
 *
 * @author ruoyi
 * @date 2023-02-02
 */
@Api(tags = "质检项")
@RestController
@RequestMapping("/quality/qualityItem")
public class QualityItemController extends BaseController {
    @Resource
    private IQualityItemService qualityItemService;

    /**
     * 查询质检项列表
     */
    @ApiOperation(value = "查询质检项列表")
    @GetMapping("/list")
    public TableDataInfo list(QualityItem qualityItem) {
        startPage();
        List<QualityItem> list = qualityItemService.selectQualityItemList(qualityItem);
        return getDataTable(list);
    }

    /**
     * 导出质检项列表
     */
    @ApiOperation(value = "导出质检项列表")
    @Log(title = "质检项", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, QualityItem qualityItem) {
        List<QualityItem> list = qualityItemService.selectQualityItemList(qualityItem);
        // 循环将值类型转为中文
        for (QualityItem item : list) {
            item.setValueType(QualityValueTypeEnum.getMessageByCode(item.getValueType()));
        }
        ExcelUtil<QualityItem> util = new ExcelUtil<QualityItem>(QualityItem.class);
        util.exportExcel(response, list, "质检项数据");
    }

    /**
     * 获取质检项详细信息
     */
    @ApiOperation(value = "获取质检项详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(qualityItemService.selectQualityItemById(id));
    }

    /**
     * 新增质检项
     */
    @ApiOperation(value = "新增质检项")
    @Log(title = "质检项", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody QualityItem qualityItem) {
        return toAjax(qualityItemService.insertQualityItem(qualityItem));
    }

    /**
     * 修改质检项
     */
    @ApiOperation(value = "修改质检项")
    @Log(title = "质检项", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody QualityItem qualityItem) {
        return toAjax(qualityItemService.updateQualityItem(qualityItem));
    }

    /**
     * 删除质检项
     */
    @ApiOperation(value = "删除质检项")
    @Log(title = "质检项", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(qualityItemService.deleteQualityItemByIds(ids));
    }


    @ApiOperation(value = "质检项导入")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<QualityItem> util = new ExcelUtil<>(QualityItem.class);
        List<QualityItem> fileDataList = util.importExcel(file.getInputStream());
        String message = this.qualityItemService.importData(fileDataList);
        return success(message);
    }
}
